package com.definesys.smecrailtransit.module.EleRecord;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseFragment;
import com.definesys.smecrailtransit.databinding.FragmentRecordBinding;
import com.definesys.smecrailtransit.module.SiteDetails.adapter.SiteDetailsParaAdapter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleParamAchievesBean;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;

import java.util.HashMap;
import java.util.WeakHashMap;

/**
 * Created by xupeizuo on 2018/4/24.
 */

public class EleRecordFragment extends SmecBaseFragment<EleRecordPresenter> implements EleRecordContract{

    public  static final String GET_ELE_RECORD_SUCCESS="GET_ELE_RECORD_SUCCESS";
    private FragmentRecordBinding fragmentRecordBinding;
    private SiteDetailsParaAdapter siteDetailsParaAdapter;
    private int stationId=12385;
    private EleRecordViewModel model;

    @Override
    protected EleRecordPresenter getPersenter() {
        return new EleRecordPresenter(getContext());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentRecordBinding=DataBindingUtil.inflate(inflater, R.layout.fragment_record,container,false);
        initView();
        return fragmentRecordBinding.getRoot();
    }

    private  void initView(){
        model=new EleRecordViewModel(getContext(),this);
        fragmentRecordBinding.setModel(model);
        siteDetailsParaAdapter = new SiteDetailsParaAdapter(getContext());
        fragmentRecordBinding.paramAchievesRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        fragmentRecordBinding.paramAchievesRecycle.setAdapter(siteDetailsParaAdapter);
        fragmentRecordBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                getELeRecord();
            }
        },500);
    }

    private void getELeRecord(){
        HashMap<String,Object> map=new HashMap<>();
        map.put("stationId",stationId);
        mPresenter.getParamAchieves(map);
    }

    public static EleRecordFragment newInstance() {
        Bundle args = new Bundle();
        EleRecordFragment fragment = new EleRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_ELE_RECORD_SUCCESS)
            }
    )
    public void getSiteELeFaultSuccess(EleParamAchievesBean eleParamAchievesBean){
        siteDetailsParaAdapter.setList(eleParamAchievesBean.getEleList());
        siteDetailsParaAdapter.notifyDataSetChanged();
    }

    @Override
    public void siteSelected(WeakHashMap map) {
        if(map.get("stationId") == null || map.get("stationId").toString().equals("")){

        }else {
            stationId=Integer.parseInt(map.get("stationId").toString());
        }

        if(map.get("stationName") == null || map.get("stationName").toString().equals("")){

        }else {
            fragmentRecordBinding.siteTitle.setText(map.get("stationName").toString());
        }
        getELeRecord();
        fragmentRecordBinding.invalidateAll();

    }
}
